---------------------------------------------------------------------------
-- 3DS MAX9.0 MaxScript Tutorial
-- Scripted by MrShaderKr
--
-- Thanks to:
-- - gildor ( http://www.gildor.org/en/projects/umodel ) for UE Viewer
-- - MaxScript Reference Manual
--
-- License:
-- - You can freely distribute it and modify it.
-- - If you find any bugs, email me to fix it.
--
-- Version history:
--
-- - (2011/05/18/Wed) - path name correction
-- - (2011/05/09/Mon) - start
--
-- Known Bugs:
-- --

include "myCommon.ms"


global g_ViewTest       = true
global g_VertexScale    = 1.0f


if g_ViewTest then
(
    clearListener()
    --heapSize += 20000000 
    myEcho "Mesh Load Test"
    
    if ( g_ModelViewFloater != undefined and g_ModelViewFloater.open ) do
    (
       closeRolloutFloater g_ModelViewFloater
    )
    
    rollout TreeViewRollout "UE Viewer Helper"
    (
        group "Folder Brower :"
        (
            button btnBrower "Select Folder" tooltip:"Select a data folder( directory )"
        )

        activeXControl tv "MSComctlLib.TreeCtrl" width:300 height:320 align:#center
        activeXControl iltv "MSComctlLib.ImageListCtrl" height:0 width:0
        
        group "Command :"
        (
            button btnView "View" tooltip:"View a selected gpk file with umodel.exe" across:2 align:#left
            button btnExtract "Extract" tooltip:"Extract a selected gpk file" align:#left
        )

        fn getIconFromBitmap thePath number =
        (
            tempBmp = openBitmap thePath
            iconBmp = bitmap 16 15
            for v = 0 to 14 do
            setPixels iconBmp [0,v] ( getPixels tempBmp [(number-1)*16, v] 16 )
            iconBmp.filename = getDir #image + "/_temp.bmp"
            save iconBmp
            close iconBmp 
            close tempBmp
            getDir #image + "/_temp.bmp"
        )

       
        fn initTreeView tv iltv =
        ( 
            tv.Indentation      = 28*15 
            tv.LineStyle        = #tvwRootLines 
            --tv.checkboxes       = true 
            --tv.sorted           = true 
            ilTv.imagewidth     = 16 
            ilTv.imageheight    = 15 
            iconDir             = ( getDir #ui ) + "\\icons"
            ilTv.listImages.add 1 #root ( loadPicture ( getIconFromBitmap ( iconDir + "\\Standard_16i.bmp" ) 2 ))
            ilTv.listImages.add 2 #geomnode (loadPicture (getIconFromBitmap (iconDir + "\\Standard_16i.bmp") 1))
            ilTv.listImages.add 3 #light (loadPicture (getIconFromBitmap (iconDir + "\\Lights_16i.bmp") 3))
            ilTv.listImages.add 4 #camera (loadPicture (getIconFromBitmap (iconDir + "\\Cameras_16i.bmp") 2))
            ilTv.listImages.add 5 #helper (loadPicture (getIconFromBitmap (iconDir + "\\Helpers_16i.bmp") 1))
            --tv.imageList        = ilTv  
        )
        
        fn addFolders tv theNode theFolder =
        (
            /*local files = getFiles ( theFolder + "\\*.*" )
            for f in files do
            (
                local fileName  = filenameFromPath f
                local newNode   = tv.Nodes.add theNode.index 4 "" fileName 0
                --newNode.text    = f
                --newNode.text    = getFilenameFile f
                --newNode.text    = filenameFromPath f
            )*/
            
            local folders       = getDirectories ( theFolder + "\\*" )
            for d in folders do
            (
                local pathNames = filterString d "\\"
                local dirName   = pathNames[ pathNames.count ]
                local newChild  = tv.Nodes.add theNode.index 4 "" dirName 0
                
                --local folders   = getDirectories ( d + "*" )
                addFolders tv newChild d
            )
            
            local files = getFiles ( theFolder + "*.*" )
            for f in files do
            (
                local fileName  = filenameFromPath f
                local newNode   = tv.Nodes.add theNode.index 4 "" fileName 0
                --newNode.text    = f
                --newNode.text    = getFilenameFile f
                --newNode.text    = filenameFromPath f
            )
        )

        fn fillInTreeView tv theFolder =
        (
            if ( tv != undefined ) then
            (
                tv.Nodes.Clear()
            )
            
            --local rootFolder = "D:\\mySolutions2010Resource\\C9Tools\\C9Extract\\"
            --local rootFolder = "D:\\mySolutions2010Resource\\C9Tools\\"
            --local rootFolder = theFolder + "\\"
            local rootFolder = theFolder
            
            theRoot         = tv.Nodes.add()
            --showProperties theRoot
            --showMethods theRoot
            --theRoot.image   = 1
            theRoot.text    = rootFolder
            
            addFolders tv theRoot rootFolder
        )
        
        fn getFullPathName theNode =
        (
            local nameArray  = #()
            append nameArray theNode.text
            
            local parentNode = theNode.parent
            while( undefined != parentNode ) do
            (
                append nameArray parentNode.text
                
                parentNode = parentNode.parent
            )
            
            local fullPath = ""
            
            for i = nameArray.count to 2 by -1 do
            (
                fullPath += nameArray[i]
                fullPath += "\\"
            )
            fullPath += nameArray[1]
            
            --myLog fullPath as string
            return fullPath
        )
        
        on tv nodeClick theNode do
        (
            local nameArray  = #()
            append nameArray theNode.text
            
            local parentNode = theNode.parent
            while( undefined != parentNode ) do
            (
                append nameArray parentNode.text
                
                parentNode = parentNode.parent
            )
            
            local fullPath = ""
            
            for i = nameArray.count to 2 by -1 do
            (
                fullPath += nameArray[i]
                fullPath += "\\"
            )
            fullPath += nameArray[1]
            
            myLog fullPath as string
        )
        
        on TreeViewRollout open do
        (
            --myLog "TreeViewRollout open"
            initTreeView tv iltv
            --fillInTreeView tv
        )
        
        on btnBrower pressed do
        (
            local targetFolder = undefined
            targetFolder = getSavePath caption:"Select a target Folder" initialDir:"D:\\myGames\\Tera\\S1Game\\CookedPC\\Art_Data\\Packages\\CH"
            
            if ( targetFolder != undefined ) then
            (
                --if ( tv != undefined ) then tv.Clear()
                --myLog ( "Selected : " + targetFolder )
                fillInTreeView tv targetFolder
            )
        )
        
        on btnView pressed do
        (
            myLog "btnView pressed"
            local selectedNode = tv.SelectedItem
            if( undefined != selectedNode ) then
            (
                local dataFullPath  = getFullPathName selectedNode
                myLog dataFullPath as string
                
                local fileType      = getFilenameType dataFullPath
                
                local sourceName    = getSourceFileName()
                local sourcePath    = getFilenamePath sourceName
                local fullPath      = sourcePath + "umodel.exe"
                
                local dataPath      = getFilenamePath dataFullPath
                local dataName      = getFilenameFile dataFullPath
                                
                if ( fileType == ".gpk" ) or ( fileType == ".GPK" ) then
                (
                    --myLog sourceName as string
                    --myLog sourcePath as string
                    --myLog fullPath as string
                    
                    local parentName = selectedNode.parent.text
                    local selfName   = selectedNode.text
                    
                    --myLog theNode.fullPath as string
                    myLog dataPath as string
                    --myLog dataName as string
                    myLog parentName as string
                    myLog selfName as string
                    
                    local exeString = "\""
                    exeString += fullPath
                    exeString += " -path="
                    exeString += dataPath
                    --exeString += parentName
                    exeString += " -view "
                    exeString += selfName
                    exeString += "\""
                    
                    local finalString = ""
                    
                    for i=1 to exeString.count do
                    (
                        if( exeString[i] == "\\" ) then
                        (
                            finalString += "/"
                        )
                        else
                        (
                            finalString += exeString[i]
                        )
                        
                    )
                    
                    myLog exeString as string
                    myLog finalString as string
                    
                    --DOSCommand fullPath
                    --DOSCommand exeString
                    DOSCommand finalString
                ) 
            )
        )
        
        on btnExtract pressed do
        (
            myLog "btnView pressed"
            local selectedNode = tv.SelectedItem
            
            if( undefined == selectedNode ) then
            (
                return undefined
            )
            
            local selectedFolder = getSavePath caption:"Select a folder !!!" --initialDir:"$scripts"
            if( undefined == selectedFolder ) then
            (
                return undefined
            )

            local dataFullPath  = getFullPathName selectedNode
            myLog dataFullPath as string
            
            local fileType      = getFilenameType dataFullPath
            
            local sourceName    = getSourceFileName()
            local sourcePath    = getFilenamePath sourceName
            local fullPath      = sourcePath + "umodel.exe"
            
            local dataPath      = getFilenamePath dataFullPath
            local dataName      = getFilenameFile dataFullPath

            if ( fileType == ".gpk" ) or ( fileType == ".GPK" ) then
            (
                local parentName = selectedNode.parent.text
                local selfName  = selectedNode.text
                
                --myLog theNode.fullPath as string
                myLog dataPath as string
                --myLog dataName as string
                --myLog parentName as string
                --myLog selfName as string
                
                local exeString = "\""
                exeString += fullPath
                exeString += " -path="
                exeString += dataPath
                --exeString += parentName
                exeString += " -out="
                exeString += selectedFolder
                exeString += " -export -all "
                exeString += selfName
                exeString += "\""
                
                local finalString = ""
                
                for i=1 to exeString.count do
                (
                    if( exeString[i] == "\\" ) then
                    (
                        finalString += "/"
                    )
                    else
                    (
                        finalString += exeString[i]
                    )
                    
                )
                
                myLog exeString as string
                myLog finalString as string
                
                --DOSCommand fullPath
                --DOSCommand exeString
                DOSCommand finalString
            ) 
           
        )
    )
    
    g_ModelViewFloater = newRolloutFloater "UE Viewer Helper RolloutFloater" 320 640
    addRollout TreeViewRollout g_ModelViewFloater
)